/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bash.compta.db4o;

import com.bash.compta.data.CategorieDepense;
import com.bash.compta.data.Compte;
import com.bash.compta.data.Depense;
import com.bash.compta.data.Login;
import com.bash.compta.data.Recapitulatif;
import com.bash.compta.data.Taux;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;
import java.util.Date;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author bash
 */
public final class MagicData {

    static ObjectSet set;

    public static Vector<String> getCategoriesNames() {
        List<CategorieDepense> cd = getAllObjects(CategorieDepense.class);
        Vector cdnames = new Vector();
        for (CategorieDepense acd : cd) {
            cdnames.add(acd.getLibele());
        }
        return cdnames;
    }

    public static ObjectSet getAllObjects(Class T) {

        set = DBTool.getInstance().db.query(T);

        return set;

    }

    public static ObjectSet getAllCategoriesLike(String libele) {

        CategorieDepense proto = new CategorieDepense(libele);

        set = DBTool.getInstance().db.queryByExample(proto);

        return set;

    }

    public static ObjectSet getAllCompteLike(String code) {
        Compte proto = new Compte(code, null, 0);
        set = DBTool.getInstance().db.queryByExample(proto);

        return set;

    }

    public static List<Compte> getAllCompteWhereCategoryLike(final String categorie) {


        List<Compte> cmptes = DBTool.getInstance().db.query(new Predicate<Compte>() {

            public boolean match(Compte c) {
                return (c.getTypeDepense().getLibele() == null ? categorie == null : c.getTypeDepense().getLibele().equals(categorie));
            }
        });

        return cmptes;
    }

    public static List<Depense> getAllDepenseWhereCategoryCompteLike(final String cle) {

        List<Depense> cmptes = DBTool.getInstance().db.query(new Predicate<Depense>() {

            public boolean match(Depense c) {
                return c.getCompte().getTypeDepense().getLibele().equals(cle);
            }
        });
        return cmptes;
    }

    public static List<Compte> getAllComptesLike(final String text, final String categorie) {


        List<Compte> cmptes = DBTool.getInstance().db.query(new Predicate<Compte>() {

            public boolean match(Compte c) {
                return (c.getLittera().contains(text) | c.getLibelle().contains(text)) & c.getTypeDepense().getLibele().equals(categorie);
            }
        });
        return cmptes;
    }

    public static List<Depense> getAllDepenseByDateAndByCategoryg(final String categ, final Date from, final Date to) {


        List<Depense> cmptes = DBTool.getInstance().db.query(new Predicate<Depense>() {

            public boolean match(Depense c) {
                return c.getCompte().getTypeDepense().getLibele().equals(categ) & c.getDate().after(from) & c.getDate().before(to);
            }
        });
        return cmptes;


    }

    public static void store(Object o) {
        DBTool.getInstance().store(o);
        System.out.println("New object created !");
    }

    public static double getLastTaux() {

        List<Taux> t = getAllObjects(Taux.class);
        if (t.isEmpty()) {
            Taux nt = new Taux("CDF", 910, new Date());
            store(nt);
            return nt.getValeur();
        }
        return t.get(t.size() - 1).getValeur();
    }

    //------------------------------------------------------------------------------------
    public static List<Login> findLoginObject(final String username, final String password) {

        List<Login> cmptes = DBTool.getInstance().db.query(new Predicate<Login>() {

            public boolean match(Login l) {
                return l.getUsername().equals(username) & l.getPassword().equals(password);
            }
        });
        return cmptes;

    }

    public static Double getTotalUSDOfDepenseType(String cat) {

        double total = 0;
        List<Depense> all;

        all = getAllDepenseWhereCategoryCompteLike(cat);

        for(Depense d:all){
            total+=d.getMontant();
        }
        return new Double(total);
    }

    public static Double getTotalCDFOfDepenseType(String cat) {
        double total = 0;
        List<Depense> all;

        all = getAllDepenseWhereCategoryCompteLike(cat);

        for(Depense d:all){
            total+=d.getMontantCDF();
        }
        return new Double(total);
    }
}
